package com.dishdex.menuhub.module;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:
 * Package: com.dishdex.menuhub.module.generator
 * Description:
 *
 * @Author:
 * @Create 2025/8/22 9:44
 * @Version 1.0
 */
public class CodeGenerator {
    public static void main(String[] args) {
        // 数据库配置
        String url = "jdbc:mysql://localhost:3306/menu?";
        String username = "root";
        String password = "w_G20030509..."; // 替换为您的数据库密码
        String domainName = "com.dishdex.";
        String projectName = "menuhub.";
        String moduleName = "app";

        Map<String, Object> customParams = new HashMap<>();
        customParams.put("domainName", domainName);
        customParams.put("projectName", projectName);
        customParams.put("moduleName", moduleName);

        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> builder
                        .outputDir(Paths.get(System.getProperty("user.dir")) + "/" + moduleName + "/src/main/java")
                )
                .packageConfig(builder -> builder
                        .parent(domainName + projectName + moduleName)
                        .entity("entity")
                        .mapper("mybatis/mapper")
                        .service("service")
                        .serviceImpl("service.impl")
                        .controller("controller")
                        .pathInfo(Collections.singletonMap(OutputFile.xml,
                                Paths.get(System.getProperty("user.dir")) + "/" + moduleName + "/src/main/resources/mapper"))
                )
                .strategyConfig(builder -> builder
                        .addInclude("category") // 设置需要生成的表名
                )
                .injectionConfig(builder -> builder
                        .customMap(customParams)
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .templateConfig(builder -> builder
                        .disable(TemplateType.CONTROLLER)
                        .disable(TemplateType.ENTITY)
                        .disable(TemplateType.SERVICE)
                        .disable(TemplateType.SERVICE_IMPL)
                        .disable(TemplateType.CONTROLLER)
                        .entity("/templates/entity.java")
                        .mapper("/templates/mapper.java")
                        .service("/templates/service.java")
                        .controller("/templates/controller.java")
                        .serviceImpl("/templates/serviceImpl.java")
                        .xml("/templates/mapper.xml")
                )


                .execute();
    }
}

